/*
 * 文件名： UserFragment.java
 * 描    述：已停止使用
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.miguo.activity.base.BaseFragment;
import com.miguo.activity.callback.IFragment;
import com.miguo.app.AppConfig;
import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.request.RequestJsonHandler;
import com.miguo.framework.view.pulltozoomview.PullToZoomScrollViewEx;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.plugins.Statistics;
import com.miguo.plugins.UmengFeedback;
import com.miguo.plugins.alibaba.BaiChuan;
import com.miguo.ui.R;
import com.miguo.view.RemindTimeTextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserCenterFragment extends BaseFragment implements OnClickListener, IFragment {
    private View view;

    private TextView mSettingTv;
    private ImageView mScanIv;

    private RemindTimeTextView mKaijiangTimeTips;
    private TextView mKaijiangTips;

    private LinearLayout mLoginLl;
    private LinearLayout mInfoll;
    private ImageView mArrowIv;

    private TextView mNameTv, mIntegralTv;

    private PullToZoomScrollViewEx mUserScrollView;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViewById();
        initView();
        initRemind();
    }

    private View mHeaderView, mRootView;

    private void findViewById() {
        /**
         * 兼容4.0以下PullToZoomScrollView的头部zoom问题
         */
        if (Build.VERSION.SDK_INT >= 14) {
            mUserScrollView = (PullToZoomScrollViewEx) view.findViewById(R.id.user_scroll_view);
            View mUserHead = LayoutInflater.from(mContext).inflate(R.layout.layer_user_header, null, false);
            View mUserContent = LayoutInflater.from(mContext).inflate(R.layout.layer_user_content, null, false);
            mUserScrollView.setHeaderView(mUserHead);
            mUserScrollView.setScrollContentView(mUserContent);

            View zoomView = LayoutInflater.from(mContext).inflate(R.layout.pull_to_zoom_profile_view, null, false);
            ImageView iv_zoom = (ImageView) zoomView.findViewById(R.id.iv_zoom);
            iv_zoom.setImageResource(R.drawable.user_profile_bg);
            mUserScrollView.setZoomView(zoomView);

            if (Build.VERSION.SDK_INT >= 9) {
                mUserScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);
            }

            mHeaderView = mUserScrollView.getHeaderView();
            mRootView = mUserScrollView.getPullRootView();
        } else {
            mHeaderView = view;
            mRootView = view;
        }

        mSettingTv = (TextView) mHeaderView.findViewById(R.id.user_setting_tv);
        mScanIv = (ImageView) mHeaderView.findViewById(R.id.user_scan_iv);
        mSettingTv.setOnClickListener(this);
        mScanIv.setOnClickListener(this);

        //Header未登录VIEW
        mLoginLl = (LinearLayout) mHeaderView.findViewById(R.id.user_login_ll);
        mHeaderView.findViewById(R.id.user_login_bt).setOnClickListener(this);
        //Header已登录VIEW
        mInfoll = (LinearLayout) mHeaderView.findViewById(R.id.user_info_ll);
        mArrowIv = (ImageView) mHeaderView.findViewById(R.id.user_arrow_iv);
        mNameTv = (TextView) mHeaderView.findViewById(R.id.user_login_name_tv);
        mIntegralTv = (TextView) mHeaderView.findViewById(R.id.user_login_integral_tv);
        mInfoll.setOnClickListener(this);

        //Content 我的订单VIEW
        //淘宝订单
        mRootView.findViewById(R.id.user_order_rl).setOnClickListener(this);
        //购物车
        mRootView.findViewById(R.id.user_cart_rl).setOnClickListener(this);
        //物流
        mRootView.findViewById(R.id.user_logistics_rl).setOnClickListener(this);
        //退款
        mRootView.findViewById(R.id.user_after_sale_rl).setOnClickListener(this);
        //帮助
        mRootView.findViewById(R.id.user_tao_help_rl).setOnClickListener(this);


        //Content 列表VIEW
        mKaijiangTimeTips = (RemindTimeTextView) mRootView.findViewById(R.id.usre_kaijiang_time_tips);
        mKaijiangTips = (TextView) mRootView.findViewById(R.id.usre_kaijiang_tips);

        //我的收藏
        mRootView.findViewById(R.id.usre_favor_rl).setOnClickListener(this);
        //我的抽奖
        mRootView.findViewById(R.id.usre_lottery_rl).setOnClickListener(this);
        //积分商城
        mRootView.findViewById(R.id.usre_credits_scoreShop_rl).setOnClickListener(this);
        //积分记录
        mRootView.findViewById(R.id.usre_credits_log_rl).setOnClickListener(this);
        //开抢提醒
        mRootView.findViewById(R.id.usre_kaijiang_rl).setOnClickListener(this);
        //意见反馈
        mRootView.findViewById(R.id.btnFeedback).setOnClickListener(this);
        //帮助中心
        mRootView.findViewById(R.id.usre_help_rl).setOnClickListener(this);
    }

    private void initView() {
        if (AppContext.UserInfo().getIsLogin()) { // 已登录
            mLoginLl.setVisibility(View.GONE);
            mInfoll.setVisibility(View.VISIBLE);
            mArrowIv.setVisibility(View.VISIBLE);
            mNameTv.setText(AppContext.UserInfo().getUsername());
            mNameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 21);
            mIntegralTv.setText("我的积分：" + AppContext.UserInfo().getScore());
        } else { // 未登录
            mLoginLl.setVisibility(View.VISIBLE);
            mInfoll.setVisibility(View.GONE);
            mArrowIv.setVisibility(View.GONE);
        }
    }

    private void initRemind() {
        List<GoodsBean> RemindList = GoodsDB.getInstance(mContext, true).findRemind();
        if (RemindList.size() > 0) {
            mKaijiangTimeTips.setVisibility(View.VISIBLE);
            mKaijiangTimeTips.setTime(RemindList.get(0).getBegin_time());
            mKaijiangTimeTips.run();
            mKaijiangTips.setText("后开始");
            mKaijiangTips.setTextSize(13);
        } else {
            mKaijiangTimeTips.setVisibility(View.GONE);
            mKaijiangTips.setText("收藏提醒");
            mKaijiangTips.setTextSize(14);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_setting_tv:
                // 设置
                AppHelper.StartSetting(mActivity);
                break;
            case R.id.user_scan_iv:
                // 扫一扫
                AppHelper.StartScanCapture(mActivity);
                break;
            case R.id.user_login_bt:
                // 登录
                AppHelper.StartLogin(mActivity);
                break;
            case R.id.user_order_rl:
                // 我的订单
                if (AppContext.isAilError) {
                    AppHelper.StartWebView(mActivity, AppConfig.TAO.ORDER);
                } else {
                    BaiChuan.showPage(mActivity, "我的订单", AppConfig.TAO.ORDER);
                }
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_28);
                break;
            case R.id.user_cart_rl:
                // 我的购物车
                //阿里百川购物车
                if (AppContext.isAilError) {
                    AppHelper.StartWebView(mActivity, AppConfig.TAO.CART);
                } else {
                    BaiChuan.showCart(mActivity);
                    //BaiChuan.showPage(mActivity, AppConfig.TAO.CART);
                }
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_26);
                break;
            case R.id.user_logistics_rl:
                // 物流信息
                if (AppContext.isAilError) {
                    AppHelper.StartWebView(mActivity, AppConfig.TAO.LOGISTICS);
                } else {
                    BaiChuan.showPage(mActivity, "物流信息", AppConfig.TAO.LOGISTICS);
                }
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_27);
                break;
            case R.id.user_after_sale_rl:
                //退款
                if (AppContext.isAilError) {
                    AppHelper.StartWebView(mActivity, AppConfig.TAO.REFUND);
                } else {
                    BaiChuan.showPage(mActivity, "退款/售后", AppConfig.TAO.REFUND);
                }
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_27);
                break;
            case R.id.user_tao_help_rl:
                //淘宝帮助
                if (AppContext.isAilError) {
                    AppHelper.StartWebView(mActivity, AppConfig.TAO.HELP);
                } else {
                    BaiChuan.showPage(mActivity, "意见反馈", AppConfig.TAO.HELP);
                }
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_27);
                break;
            case R.id.usre_favor_rl:
                // 我的收藏
                AppHelper.StartFavor(mActivity);
                break;
            case R.id.usre_lottery_rl:
                // 我的抽奖
                if (AppContext.UserInfo().getIsLogin()) {
                    AppHelper.StartChouJiangRecord(mActivity);
                } else {
                    AppHelper.StartLogin(getActivity());
                }
                break;
            case R.id.usre_credits_scoreShop_rl: //积分商城
                AppHelper.StartScoreShop(mActivity);
                break;
            case R.id.usre_credits_log_rl:
                // 积分记录
                if (AppContext.UserInfo().getIsLogin()) {
                    AppHelper.StartSignRecord(mActivity);
                } else {
                    AppHelper.StartLogin(getActivity());
                }
                break;
            case R.id.usre_kaijiang_rl:
                // 开抢提醒
                AppHelper.StartFavor(mActivity, 1);
                break;
            // 意见反馈
            case R.id.btnFeedback:
                UmengFeedback.startFeedbackActivity(mContext);
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_15);
                break;
            case R.id.usre_help_rl:
                // 帮助中心
                AppHelper.StartWebView(mActivity, AppConfig.MG.HELP);
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_46);
                break;
            case R.id.user_info_ll:
                // 用户帐户
                AppHelper.StartUserAccount(mActivity);
                break;
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user_centre, container, false);
        return view;
    }

    private void getDataLoad() {
        RequestJsonHandler AjaxCall = new RequestJsonHandler() {
            @Override
            public void onRequestFinish(JsonData json) {
                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    AppContext.UserInfo().setUserInfo(data);
                    initView();
                }
            }
        };
        Map<String, String> param = new HashMap<String, String>();
        param.put("token", AppContext.UserInfo().getToken());
        HttpClient.postEncryptData(Url.toRoute(Url.UserCentre.UserInfo, 1), param,
                AjaxCall);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Activity.RESULT_FIRST_USER
                && resultCode == Activity.RESULT_OK) {
            initView();
        }
        initRemind();
    }

    @Override
    public void onStartFragment() {
        if (view != null) {
            if (AppContext.UserInfo().getIsLogin()) {
                getDataLoad();
            } else {
                mLoginLl.setVisibility(View.VISIBLE);
                mInfoll.setVisibility(View.GONE);
                mInfoll.setVisibility(View.GONE);
                mArrowIv.setVisibility(View.GONE);
            }
        }
        Statistics.onTrackCustomClick(mContext, AppConfig.STATISTICS.EVENT_NAME_5);
    }

    @Override
    public void onStopFragment() {
    }
}
